//
//  FuelMonthHCell.m
//  carcareIOS
//
//  Created by ileo on 16/4/12.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "FuelMonthHCell.h"

#define GAP 5.5
#define CellWidth 79

@interface FuelMonthHCell ()

@property (nonatomic, strong) UILabel *monthL;//月份
@property (nonatomic, strong) CALayer *barLayer;//柱状图

@property (nonatomic, strong) UIButton *button;//点击

@property (nonatomic, assign) CGFloat barHeight;


@end

@implementation FuelMonthHCell

+(CGFloat)cellWidth{
    return CellWidth;
}

-(id)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.barAlpha = 1;
    }
    return self;
}

-(void)prepareForReuse{
    self.barLayer.bounds = CGRectMake(0, 0, CellWidth - 2 * GAP, 0);
}

-(void)configHeight:(CGFloat)height{
    self.monthL.frame = CGRectMake(0, height - 23, CellWidth, 23);
    self.barLayer.position = CGPointMake(CellWidth / 2, self.monthL.top);
    self.barLayer.bounds = CGRectMake(0, 0, CellWidth - 2 * GAP, 0);
    self.button.frame = CGRectMake(0, 0, CellWidth, height);
}

-(void)setMonth:(NSString *)month height:(CGFloat)height isChoice:(BOOL)isChoice click:(void (^)())click{
    self.monthL.text = month;
    self.barHeight = height;
    self.barLayer.bounds = CGRectMake(0, 0, CellWidth - 2 * GAP, self.barHeight);
    self.monthL.textColor = isChoice ? COLOR_HEXSTRING(@"#1f90e5") : COLOR_HEXSTRING(@"#b6b6b6");
    self.barLayer.opacity = self.barAlpha * self.barOpacity;
    [self.button onlyHangdleUIControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        if (click) click();
    }];
}

-(void)setBarOpacity:(CGFloat)barOpacity{
    _barOpacity = 1 - barOpacity * 0.4;
    self.barLayer.opacity = self.barAlpha * _barOpacity;
}

-(void)setBarAlpha:(CGFloat)barAlpha{
    _barAlpha = barAlpha;
    self.barLayer.opacity = self.barOpacity * barAlpha;
    self.barLayer.bounds = CGRectMake(0, 0, CellWidth - 2 * GAP, self.barHeight * barAlpha);
}

-(UILabel *)monthL{
    if (!_monthL) {
        _monthL = [Factory labelWithFrame:CGRectNull font:FONT_DEFAULT_Light(12) text:nil textColor:nil onView:self textAlignment:NSTextAlignmentCenter];
        _monthL.backgroundColor = [UIColor whiteColor];
    }
    return _monthL;
}

-(CALayer *)barLayer{
    if (!_barLayer) {
        _barLayer = [CALayer layer];
        _barLayer.anchorPoint = CGPointMake(0.5, 1);
        _barLayer.speed = 2;
        _barLayer.backgroundColor = COLOR_HEXSTRING(@"#a3e6ff").CGColor;
        [self.layer addSublayer:_barLayer];
    }
    return _barLayer;
}

-(UIButton *)button{
    if (!_button) {
        _button = [[UIButton buttonEmptyWithFrame:CGRectNull click:nil] setupOnView:self];
    }
    return _button;
}

@end
